/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.ts.controller;

import java.util.Collection;
import java.util.List;

import com.xy.biz.ts.domain.bo.ExaminationPaperBO;
import com.xy.biz.ts.domain.converter.ExaminationPaperQuestionConverter;
import com.xy.biz.ts.domain.converter.ExaminationPaperRuleConverter;
import com.xy.biz.ts.domain.query.ExaminationRecordQuery;
import com.xy.biz.ts.domain.req.*;
import com.xy.biz.ts.domain.req.examinationPaper.GetExaminationPaperQuestionScoreRequest;
import com.xy.biz.ts.domain.resp.GetFixedTotalQuestionAndScoreResponse;
import com.xy.biz.ts.service.ExaminationPaperQuestionService;
import com.xy.biz.ts.service.ExaminationRecordService;
import com.xy.tool.context.UserContextHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;

import com.xy.biz.ts.domain.resp.GetExaminationPaperResponse;
import com.xy.biz.ts.domain.req.examinationPaper.GetExaminationPaperRequest;
import com.xy.biz.ts.domain.req.examinationPaper.ListExaminationPaperRequest;
import com.xy.biz.ts.domain.query.ExaminationPaperQuery;
import com.xy.biz.ts.domain.converter.ExaminationPaperConverter;
import com.xy.biz.ts.client.ExaminationPaperClient;
import com.xy.biz.ts.service.ExaminationPaperService;

/**
 * @author wcs
 * @date 2021-3-8
 */
@Api(tags = "培训流程服务-试卷")
@Slf4j
@RequestMapping("/api/ts/examination_paper")
@RestController
public class ExaminationPaperController implements ExaminationPaperClient {

    @Autowired
    private ExaminationPaperService examinationPaperService;

    @Autowired
    private ExaminationPaperQuestionService examinationPaperQuestionService;

    @Autowired
    private ExaminationRecordService examinationRecordService;

    @ApiOperation("添加固定试卷")
    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save_fixed_paper")
    public Result<?> saveFixedExaminationPaper(@RequestBody SaveFixedExaminationPaperRequest request) {
        return Result.success(examinationPaperService.saveFixedExaminationPaper(ExaminationPaperConverter.toBO(request)));
    }

    @ApiOperation("获得试卷的题数和总分")
    @PostMapping(value = "/get_fixed_question_and_score")
    public Result<GetFixedTotalQuestionAndScoreResponse> getFixedTotalQuestionAndScore(GetExaminationPaperQuestionScoreRequest request) {
        return Result.success(examinationPaperService.getFixedTotalQuestionAndScore(request.getExaminationPaperId()));
    }

    @ApiOperation("完善固定试卷")
    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/refine_fixed_paper")
    public Result<?> updateFixedExaminationPaper(@RequestBody RefineFixedExaminationPaperRequest request) {
        examinationPaperService.updateExaminationPaper(ExaminationPaperConverter.toBO(request));
        return Result.success();
    }

    //初始化随机试卷
    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save_random")
    public Result<?> saveRandomExaminationPaper(@RequestBody SaveRandomExaminationPaperRequest request) {
        examinationPaperService.saveRandomExaminationPaper(ExaminationPaperConverter.toBO(request));
        return Result.success();
    }

    @ApiOperation("生成随机试卷")
    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/generate_random_paper")
    public Result<?> generateRandomExaminationPaper(@RequestBody GenerateRandomExaminationPaperRequest request) {
        return examinationPaperService.generateRandomExaminationPaper(request.getExaminationPaperId());
    }

    @ApiOperation("添加固定试题,每次添加一条")
    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save_question_single")
    public Result<?> saveExaminationPaperQuestionSingle(@RequestBody SaveExaminationPaperQuestionRequest request) {
        examinationPaperService.saveExaminationPaperQuestion(ExaminationPaperQuestionConverter.toBO(request));
        return Result.success();
    }

    @ApiOperation("编辑")
    @Validatable(groups = {UpdateChecks.class})
    @PostMapping(value = "/update")
    public Result<?> updateExaminationPaper(@RequestBody UpdateExaminationPaperRequest request) {
        return Result.success(examinationPaperService.updateExaminationPaper(ExaminationPaperConverter.toBO(request)));
    }

    @ApiOperation("修改状态")
    @Validatable(groups = {UpdateChecks.class})
    @PostMapping(value = "/update_status")
    public Result<?> updateStatusExaminationPaper(@RequestBody GetExaminationPaperStatusRequest request) {
        examinationPaperService.updateStatusExaminationPaper(request);
        return Result.success();
    }

    @ApiOperation("删除试卷")
    @Validatable
    @PostMapping(value = "/delete")
    public Result<?> deleteExaminationPaper(@RequestBody GetDeleteExaminationPaperRequest request) {
        examinationPaperService.deleteExaminationPaper(request.getExaminationPaperId());
        return Result.success();
    }

    @ApiOperation("批量删除")
    @Validatable
    @PostMapping(value = "/delete_multiple")
    public Result<?> deleteExaminationPaper(@RequestBody Collection<Long> collection) {
        return examinationPaperService.deleteExaminationPaperMultiple(collection);
    }

    @ApiOperation("删除试题")
    @Validatable
    @PostMapping(value = "/delete_question")
    public Result<?> deleteQuestionExaminationPaper(@RequestBody GetExaminationPaperQuestionRequest request) {
        return examinationPaperQuestionService.deleteExaminationPaperQuestion(request.getExaminationPaperQuestionId());
    }

    @ApiOperation("删除随机试题")
    @Validatable
    @PostMapping(value = "/delete_rule")
    public Result<?> deleteRuleExaminationPaper(@RequestBody GetExaminationPaperRuleRequest request) {
        examinationPaperService.deleteRuleExaminationPaper(request.getExaminationPaperRuleId());
        return Result.success();
    }

    @ApiOperation("获取固定试卷")
    @Validatable
    @PostMapping(value = "/get_fixed")
    public Result<GetExaminationPaperResponse> getFixedExaminationPaper(@RequestBody GetExaminationPaperRequest request) {
        return Result.success(examinationPaperService.getFixedExaminationPaper(request));
    }

    @ApiOperation("获取随机试卷")
    @Validatable
    @PostMapping(value = "/get_Random")
    public Result<GetExaminationPaperResponse> getRandomExaminationPaper(@RequestBody GetExaminationPaperRequest request) {
        return Result.success(examinationPaperService.getRandomExaminationPaper(request));
    }

    @PostMapping(value = "/page")
    @Override
    public Result<Page<GetExaminationPaperResponse>> pageExaminationPaper(@RequestBody ListExaminationPaperRequest request) {
        ExaminationPaperQuery query = ExaminationPaperConverter.toQuery(request);
        List<GetExaminationPaperResponse> list = ExaminationPaperConverter.toResponse(examinationPaperService.listExaminationPaper(query));
        int count = examinationPaperService.countExaminationPaper(query);
        return Result.success(new Page<>(list, count));
    }

    @ApiOperation("预览试卷")
    @PostMapping(value = "/preview")
    public Result<?> previewExaminationPaper(@RequestBody GetExaminationPaperRequest request) {
        return Result.success(examinationPaperService.previewExaminationPaper(request.getExaminationPaperId()));
    }

    @ApiOperation("试卷开始考试")
    @PostMapping(value = "/start_examination")
    public Result<?> startExamination(@RequestBody GetExaminationPaperRequest request) {
        ExaminationPaperBO examinationPaper = examinationPaperService.getExaminationPaper(request.getExaminationPaperId());
        int numberOfExamsAllowed = examinationRecordService.countExaminationRecord(ExaminationRecordQuery.builder()
                .examineeUserId(UserContextHolder.getUserId())
                .examinationPaperId(request.getExaminationPaperId())
                .trainId(examinationPaper.getTrainId())
                .build());
        if (numberOfExamsAllowed >= examinationPaper.getNumberOfExamsAllowed()) {
            return Result.ofErrorData("您的考试次数已经用完！");
        }
        return Result.success(examinationPaperService.previewExaminationPaper(request.getExaminationPaperId()));
    }

    @ApiOperation("删除固定试卷试题")
    @PostMapping(value = "/delete_question_single")
    public Result<?> deleteExaminationPaperQuestionSingle(@RequestBody SaveExaminationPaperQuestionRequest request) {
        return examinationPaperService.deleteExaminationPaperQuestionSingle(ExaminationPaperQuestionConverter.toBO(request));
    }

}
